package com.ysg.widget.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.widget.dialog.adapter.DialogSheetAdapter;
import com.ysg.widget.dialog.entity.DialogEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部 sheetDialog
 */
public class SheetDialog extends BaseDialog {
    public interface OnSheetListener{
        void onItemClick(DialogEntity entity);
    }
    private OnSheetListener listener;
    public void setOnSheetListener(OnSheetListener listener){
        this.listener = listener;
    }

    private TextView tvCancel;
    private RecyclerView recyclerView;
    private DialogSheetAdapter adapter;
    private List<DialogEntity> list = new ArrayList<>();

    public SheetDialog(Context context, String[] items) {
        super(context);
        for (String s: items) {
            list.add(new DialogEntity(s));
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_sheet;
    }

    public void initView() {
        setWidthScale(0.95f);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);

        tvCancel = findViewById(R.id.tvCancel);
        recyclerView = findViewById(R.id.recyclerView);
        adapter = new DialogSheetAdapter();
        YRecyclerViewUtil.init(mContext, recyclerView, adapter);
    }

    @Override
    public void initData() {
        if (YCollectionUtil.isNotEmpty(list)){
            adapter.setList(list);
        }

    }

    @Override
    public void initListener() {
        tvCancel.setOnClickListener((v -> {
            dismiss();
        }));

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                dismiss();
                if (listener != null){
                    listener.onItemClick(list.get(position));
                }
            }
        });




    }


}
